package tech.aistar.util.editnotes;

import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * checkText 类用来返回敏感词
 */
public class CheckWords {
/*
导入json
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
 */
    public static Set<Object> checkText(String mytext) throws Exception{

        String authHost = "https://aip.baidubce.com/rest/2.0/antispam/v2/spam?access_token="+ GetAccessToken.getAuth();

            HttpResponse<String> response = Unirest.post(authHost)
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .field("content",mytext )
                    .asString();
        JSONObject jsonObj = JSONObject.fromObject(response.getBody());

        Map<String, Class> classMap = new HashMap<String, Class>();
        classMap.put("result",Result.class);
        classMap.put("pass",PassWords.class);
        CheckTextResult bean = (CheckTextResult)JSONObject.toBean(jsonObj, CheckTextResult.class, classMap);
        int errorwords= bean.getResult().getReject().size(); //错别字
        int successwords= bean.getResult().getReview().size();//无措
        Set<Object> setWord=new HashSet<>();
        if(errorwords>0){
            JSONArray j= jsonObj.getJSONObject("result").getJSONArray("reject").getJSONObject(0).getJSONArray("hit");

            if(j.size()>0){
                for(int i=0;i<=j.size()-1;i++){
                    setWord.add(j.get(i));
                }
            }
        }
        return setWord;
    }



}
